/*
Copyright 2011 Christian Dadswell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package uk.co.chrisdadswell.bbcbc;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Activity_Schedules extends ListActivity {

	final static String APP_TAG = "BBCBC";
	final static String ACT_TAG = "Activity_Schedules: ";

	protected static final int DIALOG_REGION = 0;
	
	public static String selectedFrequency = null;
	public static String selectedRegion = null;
	public static String selectedPeriod = null;
	public static String selectedStation = null;
	public static String stationShortName = null;
	public static String scheduleURL = null;
	
	// LIFECYCLES
    @Override
	public void onStart() {
		super.onStart();
		Log.d(APP_TAG, ACT_TAG + "... OnStart ...");
		if(selectedRegion != null) {
			Log.d(APP_TAG, ACT_TAG + "Region already set to: " + selectedRegion);
		}else{
			Log.d(APP_TAG, ACT_TAG + "Region needs to be set, show REGIONS dialog");
			showDialog(DIALOG_REGION);			
		}
	}
    
    @Override
	public void onRestart() {
		super.onRestart();
		Log.d(APP_TAG, ACT_TAG + "... OnReStart ...");
	}
	
    @Override
	public void onResume() {
		super.onResume();
		Log.d(APP_TAG, ACT_TAG + "... OnResume ...");
	}
	
    @Override
	public void onPause() {
    	 super.onPause();
    	 Log.d(APP_TAG, ACT_TAG + "... OnPause ...");
     }
	
    @Override
    public void onStop() {
    	super.onStop();
    	Log.d(APP_TAG, ACT_TAG + "... OnStop ...");
    }
     
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	Log.d(APP_TAG, ACT_TAG + "... OnDestroy ...");
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.lists);
	  Log.d(APP_TAG, ACT_TAG + "... OnCreate ...");
	  
	  // SETUP VIEW
	  TextView wTitle = (TextView) findViewById(R.id.tv_Title);
	  wTitle.setText("Select a station to view its schedule");

	  if(selectedRegion != null) {
		  TextView wFooter = (TextView) findViewById(R.id.tv_Footer);
		  wFooter.setText("Region currently set to " + selectedRegion);
		  wFooter.invalidate();
	  }
	  
      String[] arr_stations = new String[]{Xml_MainParser.ByType(1)};
      String temp_stations = Func_Strings.arrayToString(arr_stations, "#");
      String[] STATIONS = temp_stations.split("#");
      
	  setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, STATIONS));
	  getListView().setTextFilterEnabled(true);
	  getListView().setSmoothScrollbarEnabled(true);
	  getListView().setFadingEdgeLength(60);
	  getListView().setFastScrollEnabled(true);
	  getListView().setVerticalFadingEdgeEnabled(true);
	}	
	
	@Override
	protected void onListItemClick(ListView listView, View view, int position, long id) {
	    super.onListItemClick(listView, view, position, id);
		Intent intentWebView = new Intent();
	    Object pos = this.getListAdapter().getItem(position); 
	    selectedStation = pos.toString();

	    //GET THE SELECTED STATION SHORT NAME
        stationShortName = Xml_MainDataset.GetNetworkInfo(selectedStation);
        
		intentWebView.setClass(Activity_Schedules.this, Activity_SchedulesWebView.class);
		intentWebView.putExtra("SelectedRegion", selectedRegion);
		intentWebView.putExtra("SelectedStation", selectedStation);
		intentWebView.putExtra("StationShortName", stationShortName);
	    startActivity(intentWebView);
	}

	// PREPARE OPTIONS MENU
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		menu.add(0,Constants.MENU_PREFERENCES,0,"Help / Support").setIcon(R.drawable.preferences);
		//TODO: ADD MENU TO CHANGE REGION
		return super.onPrepareOptionsMenu(menu);
	}
		
	@Override
	public boolean onOptionsItemSelected (MenuItem item){
		switch (item.getItemId()){
		case Constants.MENU_PREFERENCES:
			Intent prefsActivity = new Intent(Activity_Schedules.this, Activity_Preferences.class);
			startActivity(prefsActivity);
			break;
		}
		return false;
	}

	// REGION LIST DIALOG
    protected Dialog onCreateDialog(int id) {
    	Dialog dialog;
    	switch (id) {
        case DIALOG_REGION:
        	Log.d(APP_TAG, ACT_TAG + "DIALOG: Display Region list dialog");
        	final String[] REGION  = {"England", "N. Ireland", "Scotland", "Wales"};
        	AlertDialog.Builder region_dialog = new AlertDialog.Builder(this);
        	region_dialog.setTitle("Select Your Region");
        	region_dialog.setItems(REGION, new DialogInterface.OnClickListener() {
            	
        		public void onClick(DialogInterface dialog, int which) {
                	selectedRegion = REGION[which].toLowerCase();
                	if(selectedRegion.contains("n. ireland")) selectedRegion = "northernireland";
                	Log.d(APP_TAG, ACT_TAG + "Region selected: " + selectedRegion);

                	TextView wFooter = (TextView) findViewById(R.id.tv_Footer);
              	  	wFooter.setText("Region set to " + selectedRegion);
              	  	wFooter.invalidate();
                	dialog.dismiss();
        			return;
        		}
            }                       
            );
            
        	region_dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        		public void onCancel(DialogInterface dialog) {
        			Log.d(APP_TAG, ACT_TAG + "REGION dialog cancelled!");
                	TextView wFooter = (TextView) findViewById(R.id.tv_Footer);
              	  	wFooter.setText("No region set");
              	  	wFooter.invalidate();

        			Toast.makeText(Activity_Schedules.this, "\n\nA region must be selected\n\n", Toast.LENGTH_SHORT).show();
        			removeDialog(DIALOG_REGION);
        			showDialog(DIALOG_REGION);
        			//finish();
        		}
			});
            
            // TYPE OF DIALOG
            AlertDialog region_alert = region_dialog.create();
            return region_alert;
            
        default:
        	dialog = null;
    	}
    	return super.onCreateDialog(id);
    }
}